﻿using System;
using System.Threading;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Threading;

namespace UIWPF
{
    /// <summary>
    /// Interaction logic for MyMessageBox.xaml
    /// </summary>

    public partial class MyMessageBoxImpl : Window
    {
        private delegate void Effecting();

        private bool flag = true;
        public bool result;
        private int step = 12;
        private int timeWait = 10;

        void Initialize(string title, string content, bool YesNo, bool effect0)
        {
            if (!effect0)
            {
                Opacity = 0.0f;
            }
            else
            {
                Height = 3;
                Width = 0;
            }

            tbkTitle.Text = title;
            rtbMessage.AppendText(content);
            rtbMessage.Focus();
            if (!YesNo)
            {
                BtnMessageNo.Width = 0;
                BtnMessageNo.Height = 0;
                BtnMessageYES.Content = "OK";
            }
        }

        public MyMessageBoxImpl(string title, string content, bool YesNo, bool effect0)
        {
            InitializeComponent();
            Initialize(title, content, YesNo, effect0);
            rtbMessage.VerticalContentAlignment = VerticalAlignment.Center;
            rtbMessage.HorizontalContentAlignment = HorizontalAlignment.Center;
        }

        private void OpeningEffect()
        {
            flag = true;
            while (true)
            {
                if (flag)
                {
                    if (Width + step < 386)
                    {
                        Left -= step / 2;
                        Width += step;
                    }
                    else
                    {
                        Width = 386;
                        flag = false;
                    }
                }
                else
                {
                    if (Height + step < 215)
                    {
                        Top -= step / 2;
                        Height += step;
                    }
                    else
                    {
                        Height = 215;
                        break;
                    }
                }
                Thread.Sleep(timeWait);
            }
        }

        private void doOpening()
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Effecting(OpeningEffect));
        }

        private void ClosingEffect()
        {
            flag = true;
            while (true)
            {
                if (flag)
                {
                    if (Height - step > 3)
                    {
                        Height -= step;
                        Left += step / 2;
                    }
                    else
                    {
                        Height = 3;
                        flag = false;
                    }
                }
                else
                {
                    if (Width - step > 0)
                    {
                        Width -= step;
                        Top += step / 2;
                    }
                    else
                    {
                        Width = 0;
                        Hide();
                        break;
                    }
                }
                Thread.Sleep(timeWait);
            }
        }

        private void doClosing()
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Effecting(ClosingEffect));
        }

        private void FormLoading(object sender, RoutedEventArgs e)
        {
            doOpening();
        }

        private void rtbMessageKeyDownFunc(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                result = true;
                doClosing();
            }
        }

        private void BtnYesClick(object sender, RoutedEventArgs e)
        {
            result = true;
            doClosing();
        }

        private void BtnNoClick(object sender, RoutedEventArgs e)
        {
            result = false;
            doClosing();
        }

        private void DragWindowMouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                DragMove();
            }
        }
    }

    public static class MyMessageBox
    {
        private static MyMessageBoxImpl mmb;
        private static bool effect0 = true;

        public static void SetEffect(bool eff)
        {
            effect0 = true;
        }

        public static void Show(string content)
        {
            mmb = new MyMessageBoxImpl("Message", content, false, effect0);
            mmb.ShowDialog();
            mmb.Close();
        }

        public static bool Show(string content, bool YesNo)
        {
            mmb = new MyMessageBoxImpl("Message", content, YesNo, effect0);
            mmb.ShowDialog();

            bool res = mmb.result;
            mmb.Close();
            return res;
        }

        public static bool Show(string title, string content, bool YesNo)
        {
            mmb = new MyMessageBoxImpl(title, content, YesNo, effect0);
            mmb.ShowDialog();

            bool res = mmb.result;
            mmb.Close();
            return res;
        }
    }
}